home *** CD-ROM | disk | FTP | other *** search
- Path: fsgi01.fnal.gov!not-for-mail
- From: b91926@fsgi01.fnal.gov (David Sachs)
- Newsgroups: comp.lang.c++
- Subject: Re: Creating operator= for *pointers* to objects??
- Date: 12 Mar 1996 11:04:13 -0600
- Organization: FERMILAB, Batavia, IL
- Message-ID: <4i4aqd$e2b@fsgi01.fnal.gov>
- References: <Do4KtH.MK@mv.mv.com>
- Reply-To: sachs@fnal.fnal.gov
- NNTP-Posting-Host: fsgi01.fnal.gov
- X-Newsreader: NN version 6.5.0 #9 (NOV)
-
- bnb@curtech.com (Brian Ballard) writes:
-
-
-
- >I'm trying to find a way to create an assignment (or copy) operator
- >which I can use to apply to pointers to object. For example, say I have
- >a class hierarchy that looks like:
-
- > |--- B
- > A ---|--- C
- > |--- D
-
- >Millions of these objects can potentially be created and scrapped every
- >few minutes so, in order to improve performance and reduce memory
- >fragmentation, every class in the hierarchy has their new/delete operators
- >overloaded to use a handful of "pre-built" objects in the freestore.
- >Pretty standard stuff.
-
- >Because exact duplicates of an instantiated object can (and must) exist,
- >class A has a refCount member which allows me to keep track of how many
- >copies of a duplicated object exist out there, so I don't pull the rug
- >out from under an object that is still being used.
-
- >In general, megabytes worth of network packets are stored in a file, which
- >get read in by this software and are wrapped up in one of the above derived
- >classes (B, C, or D) depending upon the type of packet. This creates a
- >virtual "list" of sorts where I have an iterator which walks through the
- >file, returning objects of type B/C/D.
-
- >Basically, I'd like to be able to do this:
-
- >{
- > A* start = NULL;
- > A* temp = NULL;
-
- > start = mStream->GetPacketAtOffset(0L); // This might return an object B*
- > temp = start; // I need this to call my oper=
- > while ( temp = mStream->GetNextPacket(temp)) // GetNextPacket automatically
- > bla bla bla // 'delete's first A* temp
-
- >...
- >}
-
- >In the above example, I need "temp = start" to "delete temp", create a
- >duplicate of "start" and return its pointer into "temp"
-
- >Right now, I have to do this:
-
- >{
- >...
- > delete temp;
- > temp = start->Copy();
- >...
- >}
-
- >Not a big deal to it this way, but it's easy to forget over the long haul
- >that these steps need to be taken. It's much easier to do a simple pointer
- >assignment which guarantees that every pointer to an object is accounted for.
-
-
- >OK.... a bit much content for such an easy question....
-
-
- Since operators cannot be overloaded for build-in types, you may
- wish to define a "smart pointer" class to refer to your objects.
-
- You will have to define several overloaded functions in this
- class, most notably operator->.
-
- One thing you can do is make the smart pointer class a friend of
- your base class, and make the constructors and destructors of your
- base class private. This will prevent all creation of your
- controlled objects, excpet via your opinter class.
- --
- ***** Listen Americans, the IRS is your taxer, the IRS is one. *****
- David Sachs - Fermilab, HPPC MS369 - P. O. Box 500 - Batavia, IL 60510
- Voice: 1 708 840 3942 Deparment Fax: 1 708 840 3785
-